package com.ccl.iot.object;

import com.ccl.iot.object.IOTObject.IOTVariable;

public class IOTInfoWithValue extends IOTInfoWithName implements IOTVariable<String> {
	private String iValue = null;
	
	public IOTInfoWithValue(){
	}
	
	public IOTInfoWithValue(long aID, String aName, String aValue){
		super(aID, aName);
		
		iValue = aValue;
	}
	
	public IOTInfoWithValue(IOTInfoWithValue aInfo){
		super(aInfo);
		
		if (aInfo != null){
			iValue = aInfo.iValue;
		}
	}
	
	@Override
	public String getValue() {
		return iValue;
	}
	
	public boolean setValue(byte aBuffer[], int aPos, int aLength){
		if (aBuffer != null && aLength > 0 && aBuffer.length >= aPos + aLength){
			String tValue = (new String(aBuffer, 0, aLength)).trim();
			
			if (tValue.equals(iValue) == false){
				iValue = tValue;
				
				return true;
			}
		}
		
		return false;
	}

	@Override
	public boolean setValue(String aValue) {
		if (aValue != iValue){
			if (aValue != null){
				if (aValue.equals(iValue)){
					return false;
				}
			}
			
			iValue = aValue;
			
			return true;
		}else{
			return false;
		}
	}

	@Override
	public IOTInfoWithValue clone() {
		return new IOTInfoWithValue(this);
	}

	@Override
	public String toString() {
		return super.toString() + "=" + iValue;
	}

	public int compareIgnoreID(IOTInfoWithValue aInfo) {
		int tRet;
		
		if ((tRet = super.compareIgnoreID(aInfo))== 0){
			if (iValue != null){
				return iValue.compareTo(aInfo.iValue) == 0 ? 0 : 2;
			}else{
				return aInfo.iValue == null ? 0 : 2;
			}
		}
		
		return tRet;
	}
}
